//
//  UIButton+CL.m
//  IntegralMall
//
//  Created by 朱跃龙 on 2017/9/26.
//  Copyright © 2017年 朱跃龙. All rights reserved.
//

#import "UIButton+CL.h"

@implementation UIButton (CL)

+ (UIButton *)addBtnImage:(NSString *)imgName AndFrame:(CGRect)frame WithTarget:(id)target action:(SEL)action
{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    btn.frame =frame;
    return btn;
}

+ (UIButton *)addBtnImage:(NSString *)imgName WithTarget:(id)target action:(SEL)action
{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)addBtnImage:(NSString *)imgName WithBtnTitle:(NSString *)title AndTitleColor:(UIColor *)color AndTitleFont:(CGFloat)font{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn setTitleColor:color forState:UIControlStateNormal];
    
    btn.titleLabel.font =[UIFont systemFontOfSize:font];
    
    return btn;
}

+ (UIButton *)addBtnImage:(NSString *)imgName{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    
    return btn;
}


+ (UIButton *)addBtnTitle:(NSString *)title AndTitleColor:(UIColor *)color AndTitleFont:(CGFloat)font WithTarget:(id)target action:(SEL)action
{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn setTitleColor:color forState:UIControlStateNormal];
    
    btn.titleLabel.font =[UIFont systemFontOfSize:font];
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)addBtnImage:(NSString *)img WithBtnTitle:(NSString *)title AndTitleColor:(UIColor *)color AndTitleFont:(CGFloat)font WithTarget:(id)target action:(SEL)action
{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    
    [btn setTitleColor:color forState:UIControlStateNormal];
    
    btn.titleLabel.font =[UIFont systemFontOfSize:font];
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)addBtnImage:(NSString *)img AndFrame:(CGRect)frame WithTitle:(NSString *)title AndTitleColor:(UIColor *)color AndTitleFont:(CGFloat)font AndImgInsets:(UIEdgeInsets)edgeInset AndTitleEdgeInsets:(UIEdgeInsets)titleInsets AndTarget:(id)traget AndAction:(SEL)selector
{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:titleInsets];
    [btn setImageEdgeInsets:edgeInset];
    btn.titleLabel.font =[UIFont systemFontOfSize:font];
    [btn setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [btn addTarget:traget action:selector forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
+ (UIButton *)addBtnWithFrame:(CGRect)frame WithTitle:(NSString *)title WithBGImg:(NSString *)backImg WithFont:(CGFloat)font WithTitleColor:(UIColor *)color Target:(id)traget Action:(SEL)action
{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font =[UIFont systemFontOfSize:font];
    btn.titleLabel.font =[UIFont systemFontOfSize:font];
    [btn setBackgroundImage:[UIImage imageNamed:backImg] forState:UIControlStateNormal];
    [btn addTarget:traget action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

+ (UIButton *)addBtnWithFrame:(CGRect)frame WithTitle:(NSString *)title WithBGImg:(NSString *)backImg WithSelectedBGImg:(NSString *)selectImg WithFont:(CGFloat)font WithTitleColor:(UIColor *)color WithSeletedTitleColor:(UIColor *)selectColor Target:(id)traget Action:(SEL)action{
    
    UIButton *btn_click = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // 创建普通状态按钮图片
    
    btn_click.frame = frame;
    
    [btn_click setImage:[UIImage imageNamed:backImg] forState:UIControlStateNormal];
    
    // 创建高亮或选中状态按钮图片
    
    [btn_click setImage:[UIImage imageNamed:selectImg] forState:UIControlStateSelected];
    
    [btn_click setTitle:title forState:UIControlStateNormal];
    
    
    btn_click.titleLabel.font = [UIFont systemFontOfSize:font];
    
    // 设置按钮普通状态标题颜色
    
    [btn_click setTitleColor:color forState:UIControlStateNormal];
    
    // 设置按钮高亮或选中状态标题颜色
    
    [btn_click setTitleColor:selectColor forState:UIControlStateSelected];
    
    [btn_click addTarget:traget action:action forControlEvents:UIControlEventTouchUpInside];
    
    btn_click.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [btn_click setTitleEdgeInsets:UIEdgeInsetsMake(btn_click.imageView.frame.size.height + btn_click.imageView.frame.origin.y ,-btn_click.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [btn_click setImageEdgeInsets:UIEdgeInsetsMake(-5.0, 0.0,0.0, -btn_click.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
    
    
    return btn_click;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect bounds = self.bounds;
    //若原热区小于44x44，则放大热区，否则保持原大小不变
    CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(44.0 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}


@end
